package com.ms.service.module.account;

import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.login.AccountKickPlayerInfo;
import com.ms.service.module.account.message.SCharacterLoginNotify;
import com.ms.service.module.account.message.SKickPlayerMessage;
import com.ms.service.module.account.message.SSessionCloseNotify;
import com.ms.service.telnet.STelnetCmd;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.gateway.CGatewayLoadingInfo;

public class AccountAction {
	private static final Log log = LogFactory.getLog(AccountAction.class);

	@MessageType(typeCode = MessageTypes.S_ACCOUNT_GATEWAY_LOADINGINFO)
	public void gatewayLoadingInfo(CGatewayLoadingInfo message) {
		if (log.isInfoEnabled()) {
			log.info(message.name + " " + message.gatewayIP + ":" + message.gatewayPort + " player="
					+ message.playerConnectCount + " robot=" + message.robotConnectCount);
		}
		AccountCollectionInfo.getInstance().setGatewayLoadingInfo(message);
	}

	@MessageType(typeCode = MessageTypes.S_ACCOUNT_KICK_PLAYER_RESPONSE)
	public void recvKickAccountResponse(SKickPlayerMessage message) {
		AccountKickPlayerInfo info = AccountCollectionInfo.getInstance().getKickPlayerResult(
				message.getUniqueKey(), false);
		if (info == null || info.getLockObject() == null)
			return;
		info.setResult(message.getResult());
		synchronized (info.getLockObject()) {
			info.getLockObject().notify();
		}
	}

	@MessageType(typeCode = MessageTypes.S_SESSION_CLOSE_NOTIFY)
	public void recvSessionCloseNotify(SSessionCloseNotify notify) {
		// account module 異步處理將斷線後的立即玩 account id 放回可用清單中
		BasicServiceModule m = Contexts.getModule();
		if (m instanceof AccountModule) {
			AccountModule am = (AccountModule) m;
			// 交給 module 處理
			am.recvSessionCloseNotify(notify);
		}
	}

	@MessageType(typeCode = MessageTypes.S_TELNET_COMMAND)
	public void setStatRequest(STelnetCmd obj) {
		AccountModule module = (AccountModule) Contexts.getModule();
		if (module != null && obj != null && obj.guid != null) {
			module.procTelnetCommand(obj);
		}
	}
	
	@MessageType(typeCode = MessageTypes.S_GMTOOL_SWITCH_TCP_PROXY)
	public String switchTcpProxy(Integer request) {
// 2016/1/20 更換proxyList架構，因此將不需要這個api
//		int proxyID = request;
//		
//		AccountModule module = (AccountModule) Contexts.getModule();
//		module.refreshProxyList();
//		//取group ID
//		String result = ConstantCode.getName(ErrorCode.class, module.switchProxyGroup(proxyID, false));
//		
//		if(log.isInfoEnabled()){
//			log.info(MessageFormat.format("Message = {0}, Module = {1}, ProxyID = {2}, IP = {3}:{4}", 
//					"S_GMTOOL_SWITCH_TCP_PROXY" , module.getModuleName(),module.getCurProxyGroupId()));
//		}
//		
//		return result;
		return null;
	}
	
	
	@MessageType(typeCode = MessageTypes.S_GMTOOL_GET_CURRENT_ACCOUNT_SERVER_PROXY_IP)
	public String getCurrentProxyIP(Object object)
	{
		AccountModule module = (AccountModule) Contexts.getModule();
		//取IP跟PORT
//		String IP = MessageFormat.format("{0}:{1}",module.getCurProxyIp(),module.getCurProxyPort());
//		if(log.isInfoEnabled()){
//			log.info(MessageFormat.format("Message = {0}, Module = {1}, IP = {2}", 原本的好像就有寫錯，這邊format有4個代入參數，空位卻只有3個
//					"S_GMTOOL_GET_CURRENT_ACCOUNT_SERVER_PROXY_IP", module.getModuleName(), module.getCurProxyGroupId(), IP));
//		}
		
		String IP = MessageFormat.format("{0}:{1}","","");
		if(log.isInfoEnabled()){
			log.info(MessageFormat.format("Message = {0}, Module = {1}, IP = {2}", 
					"S_GMTOOL_GET_CURRENT_ACCOUNT_SERVER_PROXY_IP", module.getModuleName(), IP));
		}
		//2016/1/20 更換proxyList架構，若還會需要這個API功能需要傳proxyType跟國別才取得到，目前暫時都回傳空字串
		return IP;
	}

	@MessageType(typeCode = MessageTypes.S_ACCOUNT_CHARACTER_LOGIN_NOTIFY)
	public void revLoginModuleTrialAccountLoginSuccess(SCharacterLoginNotify loginNotify){
		BasicServiceModule m = Contexts.getModule();
		if (m instanceof AccountModule) {
			AccountModule am = (AccountModule) m;
			am.addLoginSuccessTrialAccountID(loginNotify.getAccountId());
		}
	}
}
